package StructuralPattern.FlyweightPattern;

import StructuralPattern.FlyweightPattern.Interfase.ChessPiece;

import java.util.HashMap;
import java.util.Map;

// 享元工厂类，负责管理和创建享元对象
class ChessPieceFactory {
    private final Map<String, ChessPiece> pieceMap = new HashMap<>();

    public ChessPiece getChessPiece(String color) {
        ChessPiece piece = pieceMap.get(color);

        // 如果不存在该颜色的棋子，则创建并放入缓存
        if (piece == null) {
            piece = new ConcreteChessPiece(color);
            pieceMap.put(color, piece);
        }

        return piece;
    }
}
